/**
 * Created by udi on 17-4-11.
 */


import "./markdown.js"

var swagger_markdown = function (list) {
    const get_headers = function (headers) {
        if (!headers || !headers.length) {
            return "";
        }

        const get_header = function (header) {
            return `| ${header.name}      | ${header.value}       |  ${header.required ? '必填' : ''}  |`
        }

        return `
**请求头**

| 参数名       |    参数值    |  是否必填  |
| ----------- | ----------- | -------- |
${headers.map(it => get_header(it)).join("\n")}
`
    }

    const get_param1 = function (param) {
        return `| ${param.name}      | ${param.desc}       |  ${param.in_path ? '必填' : ''}  |`

    }

    const get_params = function (def) {
        var params = def.req_params;
        if (!params || !params.length) {
            return "";
        }

        params.forEach(it => {
            if (def.path.includes("{" + it.name + "}")) {
                it.in_path = true;
            }
        })
        return `
**请求参数**

| 参数名       |    备注    |  是否路径参数 |
| ----------- | ----------- | ----------- |
${params.map(it => get_param1(it)).join("\n")}
`
    }

    const proc_json = function (json, list, deepth) {
        if (!json) {
            return;
        }

        Object.keys(json).forEach(key => {
            const it = json[key];

            if (it.type == "array") {
                list.push({name: key, type: it.items.type + "[]", deepth: deepth, desc: it.description || ""});
                proc_json(it.items.properties, list, deepth + 1);
            } else if (it.type == "object") {
                list.push({name: key, type: it.type, deepth: deepth, desc: it.description || ""});
                proc_json(it.properties, list, deepth + 1);
            } else {
                list.push({name: key, type: it.type, deepth: deepth, desc: it.description || ""});
            }
        })
    }

    const get_row = function (row, max_level) {
        var ret = [];
        for (var i = 1; i <= max_level; i++) {
            if (row.deepth == i) {
                ret.push(row.name)
            } else {
                ret.push("   ");
            }
        }

        ret.push(row.type);
        ret.push(row.desc);


        return "| " + ret.join(" | ") + " |";
    }

    const get_response = function (res) {
        if (!res) {
            return "";
        }

        const json = JSON.parse(res), list = [];
        proc_json(json.properties, list, 1)

        const max_level = list.map(it => it.deepth).max();

        if (!list.length) {
            return "";
        }

        var levels = [];
        for (var i = 2; i <= max_level; i++) {
            levels.push("  " + i + "级参数");
        }
        return `
**响应数据**

| 参数名  |  ${levels.map(it => it + "  |").join("")}   类型     |   备注    |
| --- | ${levels.map(it => " --- | ").join("")} --- | --- |
${list.map(it => get_row(it, max_level)).join("\n")}
`
    }


    const get_request_body = function (req_body) {
        if (!req_body) {
            return "";
        }

        const json = JSON.parse(req_body), list = [];

        proc_json(json.properties, list, 1)
        const max_level = list.map(it => it.deepth).max();


        if (!list.length) {
            return "";
        }

        var levels = [];
        for (var i = 2; i <= max_level; i++) {
            levels.push("  " + i + "级参数");
        }
        return `
**请求数据${json.isList ? " -- 数组[]" : ""}**

| 参数名  |  ${levels.map(it => it + "  |").join("")}   类型     |   备注    |
| --- | ${levels.map(it => " --- | ").join("")} --- | --- |
${list.map(it => get_row(it, max_level)).join("\n")}
`
    }
    const api_markdown1 = function (def) {
        return `
## ${def.title}
\`${def.method}\` \`${def.path}\`
${get_headers(def.req_headers)}${get_params(def)}${get_request_body(def.req_body_other)}${get_response(def.res_body)}
`
    }

    return list.map(it => {
            var ret = api_markdown1(it)
            return ret;
        }
    ).join("") + jv.dev.markdown_style;
}

export default  swagger_markdown
